package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;

import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.dto.CreateOrderResponse;
import com.mall.order.dto.OrderDetailRequest;
import com.mall.order.dto.OrderDetailResponse;

import com.mall.order.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.yetus.audience.InterfaceAudience;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/*
 * @description: 处理Order的相关请求
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {

    //订单核心服务
    @Reference(timeout = 3000, retries = 0, check = false)
    private OrderCoreService orderCoreService;

    //订单查询服务
    @Reference(timeout = 3000, retries = 0, check = false)
    private OrderQueryService orderQueryService;


    /*
     * @description: 创建订单
     * @return: ResponseData
     * @author: mabinbin
     * @date: 2021/8/24
     */
    @PostMapping("/order")
    public ResponseData orderCreate(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);

        //获得用户JSONObject对象，然后获得用户id
        JSONObject jsonObject = JSON.parseObject(userInfo);
        String userIdStr = jsonObject.get("uid").toString();
        long userId = Long.parseLong(userIdStr);
        request.setUserId(userId);
//        request.setUserId((long) 62);

        //设置订单id
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /*
     * @description: 查询订单详情
     * @return: com.mall.commons.result.ResponseData
     * @author: syk
     * @date: 2021/8/25
     */
    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String id,HttpServletRequest servletRequest) {
       /* String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //获得用户JSONObject对象，然后获得用户id
        JSONObject jsonObject = JSON.parseObject(userInfo);
        String username = (String)jsonObject.get("username");*/

        OrderDetailsRequest request = new OrderDetailsRequest();
        request.setOrderId(id);
        //request.setUsername(username);
        OrderDetailsResponse response = orderQueryService.getOrderDetail(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderDetailsDto orderDetailsDto = new OrderDetailsDto();
            orderDetailsDto.setGoodsList(response.getGoodsList());
            orderDetailsDto.setOrderTotal(response.getOrderTotal());
            orderDetailsDto.setStreetName(response.getStreetName());
            orderDetailsDto.setTel(response.getTel());
            orderDetailsDto.setUserId(response.getUserId());
            orderDetailsDto.setUserName(response.getUserName());
            orderDetailsDto.setOrderStatus(response.getOrderStatus());
            return new ResponseUtil().setData(orderDetailsDto);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }



    /**
     *  @description: 取消订单
     *  @author: yangbo
     *  @date: 2021/8/25
     */
    @PostMapping("/cancelOrder")
    @Anoymous//不拦截
    public ResponseData orderCancel(@RequestBody Map map){
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(String.valueOf(map.get("orderId")));

        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCode());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    /**
     *  @description: 删除订单
     *  @author: yangbo
     *  @date: 2021/8/25
     */
    @DeleteMapping("/order/{id}")
    @Anoymous//不拦截
    public ResponseData orderDelete(@PathVariable("id")String orderId){
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(orderId);
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCode());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }
    /*
     * @description: 获取用户的所有订单
     * @return: com.mall.commons.result.ResponseData
     * @author: mabinbin
     * @date: 2021/8/25
     */
    @GetMapping("/order")
    public ResponseData orderCreate(OrderListRequest orderListRequest, HttpServletRequest servletRequest) {
        //封装请求
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        orderListRequest.setUserId(uid);
//        orderListRequest.setUserId((long) 62);

        //返回响应
        OrderListResponse orderListResponse = orderCoreService.showOrder(orderListRequest);
        if (orderListResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderListDto orderListDto = new OrderListDto();
            orderListDto.setData(orderListResponse.getData());
            orderListDto.setTotal(orderListResponse.getTotal());
            return new ResponseUtil().setData(orderListDto);
        }
        return new ResponseUtil().setErrorMsg(orderListResponse.getMsg());

    }

}
